﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    public class FeeNoticeOptController : ApiController
    {
        /// <summary>
        /// 获取结算单汇总
        /// eg.：0000000001|TST1|WEB|20|1|0||0|R|1,2
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|账单id（0=所有）|结算单id（0=所有）|制单人id（空值=所有）|客户id（0=所有）|收付标志（R-收；P-付；空值=所有）|结算单pid（逗号分隔，空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/feenoticeopt")]
        public RespFeeNotice GetBizFeeNotice(string key)
        {
            RespFeeNotice resp = new RespFeeNotice();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 10)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[7];
            string strRplb = strsKeyOriginal[9].ToUpper();
            string[] strsPid = strsKeyOriginal[10].Split(',');

            int iBillid, iFnid, iCusid, iPageSize, iCurrPage;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iBillid) || !int.TryParse(strsKeyOriginal[6], out iFnid) || !int.TryParse(strsKeyOriginal[8], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetBizFeeNoticeExtInfo(strCreateid, strComcd, iPageSize, iCurrPage,iBillid,  iFnid, iCusid, strRplb, strsPid);
            return resp;
        }

        /// <summary>
        /// 更新结算单汇总信息
        /// </summary>
        [HttpPost]
        [Route("api/financial/feenoticeopt")]
        public RespFinaNullResultInfo UpdateFeeNotice(ReqFnBase _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateFeeNoticeInfo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.fnbaseinfo);
            return resp;
        }


        /// <summary>
        /// 结算单提交审批
        /// </summary>
        /// <param name="_reqInfo">结算单信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/submitfn")]
        public RespFinaNullResultInfo SubmitFnCore(ReqFnSens _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.SubmitFn(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.fncore);
            return resp;
        }

        /// <summary>
        /// 获取结算单审批流程
        /// eg.: 0000000001|TST1|WEB|5
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|结算单id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/fnrevflow")]
        public RespBillRevFlow GetFnRevFlow(string key)
        {
            RespBillRevFlow resp = new RespBillRevFlow();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iFnid;
            if (!int.TryParse(strsKeyOriginal[3], out iFnid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iFnid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。账单流水号是必需的";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetBillRevFlow(iFnid, GlobalSettings.APPLYTP_FN);
            return resp;
        }

        /// <summary>
        /// 结算确认
        /// </summary>
        /// <param name="_reqInfo">结算单信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/settlementconfirm")]
        public RespFinaNullResultInfo SettlementConfirm(ReqFnSens _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.SettlementConfirm(_reqInfo.memberid, _reqInfo.fncore);
            return resp;
        }

        /// <summary>
        /// 更新结算单打印标志
        /// </summary>
        /// <param name="_reqInfo">结算单信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/fnprint")]
        public RespFinaNullResultInfo FnPrint(ReqFnSens _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            return financialLogic.FnPrint(_reqInfo.fncore);
        }

    }
}
